﻿using System;
using Presenter.Users;

namespace SocialNetworkClient.Pages.User
{
    public partial class SignIn : System.Web.UI.Page, ISignInView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["SignInPresenter"] != null)
            {
                var presenter = Session["SignInPresenter"] as SignInPresenter;
                if (presenter != null)
                    presenter.Reload(this);
            }
            else
            {
                Session["SignInPresenter"] = new SignInPresenter(this);
            }

            if (Session["Email"] != null && Session["Password"] != null)
            {
                Page.Response.Redirect(@"~/Pages/User/Home.aspx");
            }
        }

        public void SignInLbButtonClick(object sender, EventArgs e)
        {
            if (EntitySignIn != null)
                EntitySignIn(EmailTb.Text, PasswordTb.Text);

            if (CanSignIn)
                Page.Response.Redirect(@"~/Pages/User/Home.aspx");
        }

        #region Implementation of ISignInView

        public event Action<string, string> EntitySignIn;

        public bool CanSignIn { get; set; }

        public void SetSessionParameters(int id, string email, string password)
        {
            if (Session["Email"] == null && Session["Password"] == null && Session["UserId"] == null)
            {
                Session["Email"]    = email;
                Session["Password"] = password;
                Session["UserId"]   = id.ToString();
            }
        }

        #endregion
    }
}
